<?php
/*	Created by Alexander Alexandrov aka SS
			hazg@mail.ru				*/

define('SSLIB_Html', TRUE);

function eval_exception_handler($exc)
{
	echo 'exc yes!';
}
function evalErrorHandler($errno, $errstr, $errfile, $errline)
{
	echo 'yes!';
}
	
	
class Html
{
	private static function replaceOutUrl($matches)
	{
		$matches[0] = htmlspecialchars_decode($matches[0]);
		return '<noindex><a rel="nofollow" target="_blank" href="/outlink.php?url='.urlencode($matches[0]).'">'.$matches[0].'</a></noindex>';
	}

	static function ExecFile($path)
	{
		return Html::Exec(file_get_contents($path), $path);
	}
	static function Exec($text, $filename)
	{
		
		//$buffered = (ob_get_level() > 1)?TRUE:FALSE;
		
		
		$GLOBALS['sslib_evalated_file'] = $filename;
			ob_start();	
		
		$text = str_replace('<'.'?php','<'.'?',$text);
		$text ='?'.'>'.trim($text).'<'.'?';
		eval($text);
		
		$res = ob_get_contents();
		if(ob_get_length())
			ob_end_clean();
		unset($GLOBALS['sslib_evalated_file']);
		return $res;			
	}
	
	static function ExtractTag($tag, &$text)
	{
		$res = "";
		$regex = '/<'.$tag.'>(.*)<\/'.$tag.'>/si';
		
		if(preg_match($regex, $text, $match) > 0)
		{
			$text = str_replace($match[0], "", $text);
			if(sizeof($match) > 1)
				$res = $match[1];
		}
		return $res;
	}
	static function AcivateLinks($text)
	{
		return preg_replace_callback("#(https?|ftp)://\S+[^\s.,>)\];'\"!?]#", array('Html','replaceOutUrl'),$text);
	}
		
	static function PrintTD($data, $attribs)
	{
		$s = "<td $attribs>\n";
		$s = $s . $data;
		$s = $s . "</td>\n";
		return $s;
	}
	
	static function PrintTable($data, $table_attribs, $width, $td_attribs)
	{
		$s = "<table $table_attribs>\n";
		$i1 = 0;
		for($i = 0; $i < sizeof($data); $i++)
		{
			if(is_array($td_attribs)) $td_attrib = $td_attribs[0];
			
			else $td_attrib = $td_attribs;
			
			if($i1 == 0)
			{
				$s = $s . "<tr>\n";
				$s = $s . Html::PrintTD($data[$i], $td_attrib) . "\n";		
			}
			else 
			{
				$s = $s . (Html::PrintTD($data[$i], $td_attrib)) . "\n";
			}
			
			
			if(++$i1 == $width)
			{
				if(is_array($td_attribs))
					array_push($td_attribs, array_shift($td_attribs));
				$i1 = 0;
				$s = $s . "</tr>\n";
			}
			
		}
		$s = $s . "</table>\n";
		return $s;
	}
	
}
?>